package leetcode.lcp0012;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int minTime(int[] time, int m) {
		int len = time.length;
		if (len <= m) {
			return 0;
		}
		int l = 1, r = (int) 1e9;
		int ans = 0;
		while (l <= r) {
			int mid = l + ((r - l) >> 1);
			int take = 1;
			int sum = 0, max = 0;
			boolean delete = true;
			for (int i = 0; i < time.length;) {
				int t = time[i];
				sum += t;
				max = Math.max(max, t);
				if (sum > mid) {
					if (delete) {
						sum -= max;
						delete = false;
						i++;
					} else {
						sum = 0;
						delete = true;
						max = 0;
						take++;
					}
				} else {
					i++;
				}
			}
			if (take <= m) {
				ans = mid;
				r = mid - 1;
			} else {
				l = mid + 1;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(3, this.minTime(H.str2array3("[1,2,3,3]"), 2));
		H.compare(3, this.minTime(H.str2array3("[1,2,3,3,3]"), 2));
		H.compare(1, this.minTime(H.str2array3("[1,2,3]"), 2));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
